home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Essentials / MacApp Documentation / MacApp AppleLink Messages / MacApp.Tech$ 12⁄8⁄89 / MF&Notif ƒ / MFAndNotification.p < prev    next >
Encoding:
Text File  |  1989-04-24  |  10.2 KB  |  410 lines  |  [TEXT/PJMM]

  1.  
  2. {•                                                                            •}
  3. {•                            Auteur: Eric Carrasco                            •}
  4. {•                                    © 1989                                    •}
  5. {•                            Société CFJ Consultant                            •}
  6. {•                    Conseil, Développement, Formations                        •}
  7. {•                            Paris, le Lundi 24 Avril 1989                    •}
  8. {•                                                                            •}
  9.  
  10.  
  11. {•                                                                            •}
  12. {• Démonstration d'application adaptée à MultiFinder.                        •}
  13. {• Utilisation du Notification Manager lors du passage en arrière plan        •}
  14.  
  15.  
  16. PROGRAM MFAndNotificationDemo;
  17.  
  18. { Variables de compilation conditionnelle }
  19. {$DECL MPW }
  20. {$SETC MPW := 0 }
  21.     { 0 : THINK Pascal ;  1 : MPW  }
  22.  
  23. {$DECL Debug }
  24. {$SETC Debug := 1 }
  25.     { 0 : Final ;  1 : Debugging }
  26.  
  27.  
  28. {$IFC MPW}
  29.  
  30.     USES
  31.         MemTypes, Quickdraw, OsIntf, ToolIntf, {standard lib}
  32.         PackIntf, SANE, Notification;
  33.  
  34.  
  35.     PROCEDURE _DataInit;
  36.     EXTERNAL;
  37.  
  38.     {$IFC Debug}
  39.         {$D+}
  40.         { Trace des procédures }
  41.         {$L+}
  42.         { Listing du programme autorisé }
  43.     {$ELSEC}
  44.         {$D-}
  45.         { Suppression des noms de procédures }
  46.         {$L-}
  47.         { Pas de listing des Units }
  48.     {$ENDC}
  49.     { END IF Debug }
  50.  
  51. {$ELSEC}
  52.  
  53.     USES
  54.         Notification;
  55.     {$I-}
  56.     { Pas d'initialisation automatique avec THINK Pascal }
  57.  
  58. {$ENDC}
  59. { END IF MPW }
  60.  
  61.  
  62. {$IFC Debug}
  63.  
  64.     PROCEDURE MacsBugPrint (VAR BugStr: Str255);
  65.     INLINE
  66.         $ABFF;
  67.  
  68.     PROCEDURE Dump (S: Str255; Err: LONGINT; Int: LONGINT);
  69.         VAR
  70.             tmpSa: Str255;
  71.             tmpSb: Str255;
  72.     BEGIN
  73.         NumToString(Err, tmpSa);
  74.         NumToString(Int, tmpSb);
  75.         S := CONCAT('From : ', S, ' -- Param1= ', tmpSa, ' -- Param2= ', tmpSb);
  76.         MacsBugPrint(S);
  77.     END; { Dump }
  78.  
  79. {$ENDC}
  80. { END IF Debug }
  81.  
  82.  
  83.     CONST
  84. { SysEnvirons }
  85.         versionRequested = 1;
  86. { MultiFinder }
  87.         kSuspendOrResume = $01;
  88.         kMouseMovedMessage = $FA;
  89. { WaitNextEvent }
  90.         foreGroundSleep = 1; {temps maximum donné aux autres applis (en tics) }
  91.         backGroundSleep = 50; {temps maximum donné aux autres applis (en tics) }
  92.  { Count }
  93.         theDialogID = 128;
  94.         line1 = 15;
  95.         line2 = 30;
  96.         line3 = 45;
  97.         line4 = 60;
  98.         line5 = 75;
  99. { Gestion des alertes }
  100.         theALRTid = 129;
  101.  
  102.     VAR
  103. { MultiFinder }
  104.         MultiFinderActive, WNEIsImplemented: Boolean;
  105. { SysEnvirons }
  106.         theWorld: SysEnvRec;
  107. { Gestion des erreurs }
  108.         err: OSErr;
  109. { WaitNextEvent }
  110.         Done: Boolean;
  111.         sleep: LongInt;
  112.         RegainControl, convertClipboard: Boolean;
  113.  { Count }
  114.         i: LongInt;
  115.         theDialog: DialogPtr;
  116.         lastState: Boolean;
  117.         aRect, bRect, cRect, dRect, eRect: Rect;
  118.         aString: Str255;
  119.  { Notification }
  120.         aNMRec: NMRec; { System 6.0 and higher }
  121.         notifyLevel: integer;
  122.         nmString: str255;
  123.  
  124.  
  125. {------------------------------------------ nmResponse ---------------------------------------}
  126. { Response procedure for Notification Manager (System ≥ 6.0). }
  127.     PROCEDURE nmResponse (aNmReqPtr: QElemPtr);
  128.     BEGIN
  129.     { We won't do anything }
  130.     END; { PROC nmResponse }
  131.  
  132.  
  133. {------------------------------------------ notify ---------------------------------------}
  134. { Response procedure for Notification Manager (System ≥ 6.0). }
  135.  
  136.     PROCEDURE notify;
  137.     BEGIN
  138.         IF theWorld.systemVersion < $0600 THEN
  139.             BEGIN
  140.                 IF notifyLevel > 0 THEN
  141.                     SysBeep(5);
  142.                 EXIT(notify);
  143.             END; { IF theWorld.systemVersion < $0600 }
  144.  
  145.         WITH aNMRec DO
  146.             BEGIN
  147.                 qType := 8; { 8 = ORD(nmType) }
  148.                 IF notifyLevel > 1 THEN
  149.                     BEGIN
  150.                         nmSIcon := GetResource('SICN', 128);
  151.                         nmString := CONCAT('Message form Eric, using the Notification Manager:', CHR($0D), 'Note that this message comes from a backGround application.', CHR($0D), ' Note also the Apple menu : an Eiffel tour is blinking and a "" marks the application name.');
  152.                         nmStr := @nmString;
  153.                     END;
  154.  
  155.                 IF ODD(notifyLevel) THEN
  156.                     nmSound := Handle(ORD4(-1));
  157.             END; { WITH aNMRec }
  158.  
  159.         Err := NMInstall(@aNMRec);
  160.     END; { PROC notify }
  161.  
  162.  
  163. {------------------------------------------ CheckNotification ---------------------------------------}
  164.     PROCEDURE CheckNotification;
  165.  
  166.     BEGIN
  167.         WITH aNMRec DO
  168.             BEGIN
  169.                 qType := 0;            { queue type -- ORD(nmType) = 8 when in use }
  170.                 nmMark := 1;        { item to mark in Apple menu }
  171.                 nmSIcon := NIL;        { handle to small icon }
  172.                 nmSound := NIL;        { handle to sound record }
  173.                 nmStr := NIL;        { string to appear in alert }
  174.                 nmResp := @nmResponse;{ pointer to response routine }
  175.                 nmRefCon := 0;        { for application use }
  176.             END; { WITH aNMRec }
  177.  
  178.         IF NOT MultiFinderActive THEN
  179.             notifyLevel := 0
  180.         ELSE IF theWorld.systemVersion < $0600 THEN
  181.             notifyLevel := 1
  182.         ELSE
  183.             notifyLevel := 3;
  184.  
  185.     END; { PROC CheckNotification }
  186.  
  187.  
  188. {----------------------------------------- doUpdate --------------------------------------}
  189.     PROCEDURE doUpDate;
  190.  
  191.     BEGIN
  192.         BeginUpdate(theDialog);
  193.         EraseRect(aRect);
  194.         MoveTo(5, line1);
  195.         IF WNEIsImplemented THEN
  196.             DrawString('WaitNextEvent is implemented')
  197.         ELSE
  198.             DrawString('WaitNextEvent is not implemented');
  199.         EraseRect(bRect);
  200.         MoveTo(5, line2);
  201.         IF MultiFinderActive THEN
  202.             DrawString('MultiFinder is active')
  203.         ELSE
  204.             DrawString('MultiFinder is not active');
  205.         EraseRect(dRect);
  206.         NumToString(Sleep, aString);
  207.         MoveTo(5, line4);
  208.         IF RegainControl THEN
  209.             BEGIN
  210.                 DrawString(CONCAT('foreGround, sleep time =', aString));
  211.             END
  212.         ELSE
  213.             BEGIN
  214.                 DrawString(CONCAT('backGround, sleep time =', aString));
  215.             END; { IF RegainControl }
  216.         EndUpdate(theDialog);
  217.         EraseRect(eRect);
  218.     END; { PROC doUpdate }
  219.  
  220.  
  221. {------------------------------------------- Count ----------------------------------------}
  222. { Procedure très mal écrite }
  223.     PROCEDURE Count (i: LongInt);
  224.     BEGIN
  225.         IF theDialog = NIL THEN
  226.             Exit(Count);
  227.         EraseRect(cRect);
  228.         NumToString(i, aString);
  229.         MoveTo(5, line3);
  230.         DrawString(aString);
  231.         IF lastState <> RegainControl THEN
  232.             BEGIN
  233.                 EraseRect(dRect);
  234.                 lastState := RegainControl;
  235.                 NumToString(Sleep, aString);
  236.                 MoveTo(5, line4);
  237.                 IF RegainControl THEN
  238.                     BEGIN
  239.                         DrawString(CONCAT('foreGround, sleep time =', aString));
  240.                     END
  241.                 ELSE
  242.                     BEGIN
  243.                         DrawString(CONCAT('backGround, sleep time =', aString));
  244.                     END; { IF RegainControl }
  245.             END; { IF lastState <> RegainControl }
  246.     END; { PROC Count }
  247.  
  248.  
  249. {------------------------------------------- MainEventLoop ----------------------------------------}
  250.     PROCEDURE MainEventLoop;
  251.  
  252.         VAR
  253.     { WaitNextEvent }
  254.             EventFlag: boolean;
  255.             Event: EventRecord;
  256.     BEGIN
  257.         i := 0;
  258.         REPEAT
  259.  
  260.             i := SUCC(i);
  261.             Count(i);
  262.  
  263.             IF WNEIsImplemented THEN
  264.                 EventFlag := WaitNextEvent(EveryEvent, Event, sleep, NIL)
  265.             ELSE
  266.                 BEGIN
  267.                     SystemTask;
  268.                     EventFlag := GetNextEvent(everyEvent, Event);
  269.                 END;
  270.  
  271.             IF EventFlag THEN        {HandleEvent}
  272.                 CASE Event.what OF
  273.                     keyDown: 
  274.                         BEGIN
  275.                             Done := TRUE;
  276.                         END; { keyDown }
  277.  
  278.                     updateEvt: 
  279.                         BEGIN
  280.                             doUpdate;
  281.                         END; { updateEvt }
  282.  
  283.                     app4Evt: 
  284.                         BEGIN
  285.             (* Récupérons l'octet de poid fort (bits 24-31) *)
  286.                             CASE BSR(BAND(Event.message, $FF000000), 24) OF
  287.                                 kSuspendOrResume: 
  288.                                     BEGIN
  289.                 (* Si "message" est pair, nous recevons un resumeEvt, *)
  290.                 (* sinon un suspendEvt *)
  291.                                         RegainControl := Odd(Event.message);
  292.                 (* devons-nous coercer le presse-papiers? *)
  293.                                         convertClipboard := BAND(Event.message, $00000002) <> 0;
  294.  
  295.                                         EraseRect(eRect);
  296.                                         MoveTo(5, line5);
  297.                                         IF RegainControl THEN
  298.                                             BEGIN
  299.                         (* Nous reprenons le contrôle *)
  300.                                                 DrawString('Resume');
  301.                                                 sleep := foreGroundSleep;
  302.                                                 Err := NMRemove(@aNMRec);
  303.                                             END
  304.                                         ELSE
  305.                                             BEGIN
  306.                     (* Nous perdons le contrôle *)
  307.                                                 DrawString('Suspend');
  308.                                                 sleep := backGroundSleep;
  309.                                                 notify;
  310.                                             END; { IF RegainControl }
  311.                                     END; (* kSuspendOrResume *)
  312.  
  313.                                 kMouseMovedMessage: 
  314.                                     BEGIN
  315. {•     AdjusCursor;•}
  316.                                     END; (* kMouseMovedMessage *)
  317.                             END; (* CASE BSR(BAND(message, $FF000000), 24) *)
  318.                         END; { app4Evt }
  319.                 END; { CASE Event.what }
  320.         UNTIL Done OR (i > 2147483647 - 1);
  321.     END; { PROC MainEventLoop }
  322.  
  323.  
  324. {------------------------------------------- CheckMultiFinder ----------------------------------------}
  325.     PROCEDURE CheckMultiFinder;
  326.  
  327.         CONST
  328.             WNETrapNum = $01;
  329.             MFTrapNum = $8F;
  330.             UnImplTrapNum = $9F; (* Cette trap ne sera jamais implementée *)
  331.  
  332.     BEGIN
  333.     (* Vérifier si la version du system est supérieure à 4.1 *)
  334.     (* Sinon, MultiFinder ne pourrait fonctionner *)
  335.         WITH theWorld DO
  336.             BEGIN
  337.                 IF (Err <> noErr) OR (systemVersion < $0410) THEN
  338.                     BEGIN
  339.                         MultiFinderActive := FALSE;
  340.                         Err := StopAlert(theALRTid, NIL);
  341.                         ExitToShell; (* Nous avons tant besoin du system 4.1! *)
  342.                     END
  343.                 ELSE
  344.                     BEGIN
  345.         (* Savoir si WaitNextEvent est implémenté peut toujours etre utile *)
  346.                         WNEIsImplemented := (machineType >= 0) AND (NGetTrapAddress(WNETrapNum, ToolTrap) <> NGetTrapAddress(UnImplTrapNum, ToolTrap));
  347.         (* Technique à revoir à partir du system 7.0 *)
  348.                         MultiFinderActive := (machineType >= 0) AND (NGetTrapAddress(MFTrapNum, ToolTrap) <> NGetTrapAddress(UnImplTrapNum, ToolTrap));
  349.                     END; (* IF (Err <> noErr) OR (systemVersion < $0410) *)
  350.             END; (* WITH theWorld DO *)
  351.     END; { PROC CheckMultiFinder }
  352.  
  353.  
  354. BEGIN { PROGRAM MFAndNotificationDemo }
  355.  
  356. { Initialization (impératives pour Mac II avec LSP) }
  357. {$IFC MPW}
  358.     UnLoadSeg(@_DataInit);            {remove MPW data initialization code before any allocations}
  359. {$ENDC}
  360. { END IF MPW }
  361.     InitGraf(@thePort);                 {initialize QuickDraw}
  362.     InitFonts;                             {initialize Font Manager}
  363.     FlushEvents(everyEvent, 0);          {call OS Event Mgr to discard any previous events}
  364.     InitWindows;                         {initialize Window Manager}
  365.     InitMenus;                             {initialize Menu Manager}
  366.     TEInit;                                 {initialize TextEdit}
  367.     InitDialogs(NIL);                    {initialize Dialog Manager}
  368.     InitCursor;                               {call QuickDraw to make cursor (pointer) an arrow}
  369.  
  370.     err := SysEnvirons(versionRequested, theWorld);
  371.  
  372.     Done := FALSE;
  373.     sleep := foreGroundSleep;
  374.     RegainControl := TRUE;
  375.     lastState := NOT RegainControl;
  376.  
  377.     theDialog := NIL;
  378.     theDialog := GetNewDialog(theDialogID, NIL, Pointer(-1));
  379.     IF theDialog = NIL THEN
  380.         BEGIN
  381.             SysBeep(5);
  382.             SysBeep(5);
  383.             SysBeep(5);
  384.             ExitToShell;
  385.         END; { IF theDialog = NIL }
  386.     SetPort(theDialog);
  387.     ShowWindow(theDialog);
  388.  
  389.     CheckMultiFinder;
  390.     CheckNotification;
  391.  
  392.     aRect := theDialog^.PortRect;
  393.     EraseRect(aRect);
  394.     bRect := aRect;
  395.     cRect := aRect;
  396.     dRect := aRect;
  397.     eRect := aRect;
  398.     aRect.bottom := line1 + 3;
  399.     bRect.top := line1 + 3;
  400.     bRect.bottom := line2 + 3;
  401.     cRect.top := line2 + 3;
  402.     cRect.bottom := line3 + 3;
  403.     dRect.top := line3 + 3;
  404.     dRect.bottom := line4 + 3;
  405.     eRect.top := line4 + 3;
  406.     eRect.bottom := line5 + 3;
  407.  
  408.     MainEventLoop;
  409.  
  410. END. { PROGRAM MFAndNotificationDemo }